package com.llkj.qingnianguoxue.mine;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.adapter.ZhiBoRechargeAdapter;
import com.llkj.qingnianguoxue.base.BaseActivity;
import com.llkj.qingnianguoxue.base.ConstantUserInfo;
import com.llkj.qingnianguoxue.base.MyApplication;
import com.llkj.qingnianguoxue.bean.BalanceBean;
import com.llkj.qingnianguoxue.bean.RechargeBean;
import com.llkj.qingnianguoxue.bean.ResponseBean;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.util.DisplayUtil;
import com.llkj.qingnianguoxue.util.GridSpacingItemDecoration;
import com.llkj.qingnianguoxue.util.GsonUtil;
import com.llkj.qingnianguoxue.util.PayMethodViewUtil;
import com.llkj.qingnianguoxue.util.StringUtils;
import com.llkj.qingnianguoxue.util.TextViewUtil;
import com.llkj.qingnianguoxue.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 账户余额
 */
public class BalanceActivity extends BaseActivity implements PayMethodViewUtil.PaySuccessListener{
    @BindView(R.id.tv_balance)
    TextView tvBalance;//余额
    @BindView(R.id.tv_withdrawal)
    TextView tvWithDrawal;//提现

    @BindView(R.id.rvRechargeGrid)
    RecyclerView mRechargeGrid;
    private ZhiBoRechargeAdapter mRechargeAdapter;//充值适配器
    private List<RechargeBean> mRechargeData;
    private int mCurrentPosition;

    TextView btnRecharge;//充值
    private PayMethodViewUtil payMethodViewUtil;
    private BalanceBean balanceBean;
    private String mPayMoney;//支付金额
    private int payDuration;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_balance);
        setTitle("账户余额", true, TITLE_TYPE_IMG, R.drawable.icon_back, true, TITLE_TYPE_IMG, R.drawable.icon_balance_record);
        ButterKnife.bind(this);

        initView();
        initListener();

    }

    @Override
    protected void onResume() {
        super.onResume();
        loadWaitDialog();
        HttpMethod.userBalance(this, this,"1");
    }

    private void initView() {
        initStatusbar(TSATUSBAR_TYPE_BLACK_TEXT,rlTitle,R.color.gray_bar);
        tvWithDrawal.getPaint().setFlags(Paint. UNDERLINE_TEXT_FLAG );
        mRechargeGrid.setLayoutManager(new GridLayoutManager(this, 3));
        mRechargeGrid.addItemDecoration(new GridSpacingItemDecoration(3, DisplayUtil.dip2px(this,5), false));
        mRechargeData = new ArrayList<>();
        mRechargeAdapter = new ZhiBoRechargeAdapter(R.layout.item_master_pay,mRechargeData);
        mRechargeGrid.setAdapter(mRechargeAdapter);

        String balance = MyApplication.getInstance().sputil.getString(ConstantUserInfo.USER_BALANCE);
        setBalance(balance);

        payMethodViewUtil = new PayMethodViewUtil(this,this);
    }

    private void initListener(){
        mRechargeGrid.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                mCurrentPosition = i;
                mRechargeAdapter.setCheckPosition(i);
                RechargeBean rechargeBean = mRechargeData.get(mCurrentPosition);
                mPayMoney = rechargeBean.getPrice();
            }
        });
    }

    @Override
    protected void rightDoWhat() {
        super.rightDoWhat();
        startActivity(CostRecordActivity.class);
    }

    @OnClick({R.id.tv_recharge, R.id.tv_withdrawal})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_recharge://充值
                if(StringUtils.isEmpty(mPayMoney)){
                    ToastUtil.makeLongText(this,"请选择充值金额");
                    return;
                }
                payDuration = 0;
                payMethodViewUtil.showPayMethodDialog(mPayMoney,1);
                break;
            case R.id.tv_withdrawal:
                startActivity(WithRawalActivity.class);
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        payMethodViewUtil.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void paySucessResponse(int type) {
        //处理三方支付成功后的操作
        if (payDuration == 0) {//充值
            loadWaitDialog();
            HttpMethod.orderRecharge(this, this, mPayMoney);
        }
    }

    @Override
    protected void onhttpSuccess(String data, int method) {
        super.onhttpSuccess(data, method);

        if (method == HttpMethod.HTTP_USER_BALANCE) {//8.8 我的余额
            balanceBean = GsonUtil.GsonToBean(data, BalanceBean.class);
            if (balanceBean != null) {
                MyApplication.getInstance().sputil.put(ConstantUserInfo.USER_BALANCE, balanceBean.getBalance());
                MyApplication.getInstance().sputil.put(ConstantUserInfo.USER_WITHDRAWALS, balanceBean.getWithdrawals());
                setBalance(balanceBean.getBalance());
                if(balanceBean.getPrice()!=null){
                    mRechargeData.clear();
                    mRechargeData.addAll(balanceBean.getPrice());
                    mRechargeAdapter.notifyDataSetChanged();
                    if(mRechargeData.size()>0){
                        RechargeBean rechargeBean = mRechargeData.get(0);
                        mPayMoney = rechargeBean.getPrice();
                    }
                }
            }
        } else if (method == HttpMethod.HTTP_ORDER_RECHARGE) {//充值
            ToastUtil.makeLongText(this, "充值成功");
            ResponseBean responseBean = GsonUtil.GsonToBean(data, ResponseBean.class);
            MyApplication.getInstance().sputil.put(ConstantUserInfo.USER_BALANCE, responseBean.getBalance());
            setBalance(responseBean.getBalance());
        }
    }

    private void setBalance(String balance) {
        if (!StringUtils.isEmpty(balance)) {
            balance = balance + "银子";
            SpannableString balanceSpan = TextViewUtil.getSizeSpanSpToPx(this, balance, balance.length() - 2, balance.length(), 16);
            tvBalance.setText(balanceSpan);
        }
    }

    @Override
    protected void onhttpFailed(String message, int method) {
        super.onhttpFailed(message, method);

        if (!StringUtils.isEmpty(message)) {
            ToastUtil.makeLongText(this, message);
        }
    }

}
